<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>go语言标准库-fmt | haijd</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="Golang入门" />
  
  
  
  
  <meta name="description" content="Go标准库-fmt">
<meta name="keywords" content="Golang入门">
<meta property="og:type" content="article">
<meta property="og:title" content="Go语言标准库-fmt">
<meta property="og:url" content="http://www.hais2.com/2017/08/23/Go标准库-fmt/index.html">
<meta property="og:site_name" content="haijd">
<meta property="og:description" content="Go标准库-fmt">
<meta property="og:locale" content="default">
<meta property="og:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503503176164&di=3178bb0b334068f911022455b9f48b80&imgtype=0&src=http%3A%2F%2F7viirv.com1.z0.glb.clouddn.com%2Fgolang.jpg">
<meta property="og:updated_time" content="2017-09-05T14:47:12.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Go语言标准库-fmt">
<meta name="twitter:description" content="Go标准库-fmt">
<meta name="twitter:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503503176164&di=3178bb0b334068f911022455b9f48b80&imgtype=0&src=http%3A%2F%2F7viirv.com1.z0.glb.clouddn.com%2Fgolang.jpg">
  
    <link rel="alternate" href="/atom.xml" title="haijd" type="application/atom+xml">
  
  <link rel="icon" href="/css/images/favicon.ico">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/9749f0/00000000000000000001008f/27/l?subset_id=2&fvd=n5) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/90cf9f/000000000000000000010091/27/l?subset_id=2&fvd=n7) format("woff2");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/8a5494/000000000000000000013365/27/l?subset_id=2&fvd=n4) format("woff2");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt;src:url(https://use.typekit.net/af/d337d8/000000000000000000010095/27/l?subset_id=2&fvd=i4) format("woff2");font-weight:400;font-style:italic;}</style>
    
  <link rel="stylesheet" id="athemes-headings-fonts-css" href="//fonts.googleapis.com/css?family=Yanone+Kaffeesatz%3A200%2C300%2C400%2C700&amp;ver=4.6.1" type="text/css" media="all">
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css" >
  <link rel="stylesheet" href="/css/hiero.css" >
  <link rel="stylesheet" href="/css/glyphs.css" >
  
    <link rel="stylesheet" href="/css/vdonate.css" >
  

</head>

<script>
var themeMenus = {};

  themeMenus["/"] = "Home"; 

  themeMenus["/archives"] = "Archives"; 

  themeMenus["/categories"] = "Categories"; 

  themeMenus["/tags"] = "Tags"; 

  themeMenus["/about"] = "About"; 

</script>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  <header id="allheader" class="site-header" role="banner">
  <div class="clearfix container">
      <div class="site-branding">

          <h1 class="site-title">
            
              <a href="/" title="haijd" rel="home"> haijd </a>
            
          </h1>

          
            <div class="site-description">Stay Hungry,Stay Foolish</div>
          
            
          <nav id="main-navigation" class="main-navigation" role="navigation">
            <a class="nav-open">Menu</a>
            <a class="nav-close">Close</a>
            <div class="clearfix sf-menu">

              <ul id="main-nav" class="nmenu sf-js-enabled">
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/">Home</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/archives">Archives</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/categories">Categories</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/tags">Tags</a> </li>
                    
                      <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1663"> <a class="" href="/about">About</a> </li>
                    
              </ul>
            </div>
          </nav>


      </div>
  </div>
</header>




  <div id="container">
    <div id="wrap">
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-Go标准库-fmt" style="width: 66%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="clearfix post-1016 post type-post status-publish format-standard has-post-thumbnail hentry category-template-2 category-uncategorized tag-codex tag-edge-case tag-featured-image tag-image tag-template">
    
<div class="article-gallery">
  <div class="article-gallery-photos">
    
      <a class="article-gallery-img fancybox" href="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503503176164&di=3178bb0b334068f911022455b9f48b80&imgtype=0&src=http%3A%2F%2F7viirv.com1.z0.glb.clouddn.com%2Fgolang.jpg" rel="gallery_cjie4nafw003i3swa1hkv7exn">
        <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503503176164&di=3178bb0b334068f911022455b9f48b80&imgtype=0&src=http%3A%2F%2F7viirv.com1.z0.glb.clouddn.com%2Fgolang.jpg" itemprop="image">
      </a>
    
  </div>
</div>

    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      Go语言标准库-fmt
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	Posted on <a href="/2017/08/23/Go标准库-fmt/" class="article-date">
	  <time datetime="2017-08-23T12:56:34.000Z" itemprop="datePublished">August 23, 2017</time>
	</a>

      
	<span id="busuanzi_container_page_pv">
	  本文总阅读量<span id="busuanzi_value_page_pv"></span>次
	</span>

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="fmt包"><a href="#fmt包" class="headerlink" title="fmt包"></a>fmt包</h2><ul>
<li>fmt包实现了类似c语言的<code>printf</code>和<code>scanf</code>函数，实现格式化I/O函数。</li>
</ul>
<h3 id="printf函数"><a href="#printf函数" class="headerlink" title="printf函数"></a>printf函数</h3><p><strong>示例类型和变量</strong><br><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">type</span> Human <span class="keyword">struct</span>&#123;</span><br><span class="line">    Name <span class="keyword">string</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="keyword">var</span> people = Human&#123;Name:<span class="string">"zhangsan"</span>&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<a id="more"></a>
<p><strong>1. 普通占位符</strong>  </p>
<table>
<thead>
<tr>
<th style="text-align:left">占位符</th>
<th style="text-align:left">说明</th>
<th style="text-align:left">举例</th>
<th style="text-align:left">输出</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>%v</code></td>
<td style="text-align:left">相应值的默认格式</td>
<td style="text-align:left"><code>Printf(&quot;%v&quot;,people)</code></td>
<td style="text-align:left"><code>{zhangsan}</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%+v</code></td>
<td style="text-align:left">打印结构体时，会添加字段名显示</td>
<td style="text-align:left"><code>Printf(&quot;%+v&quot;,people)</code></td>
<td style="text-align:left"><code>{Name:zhangsan}</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%#v</code></td>
<td style="text-align:left">相应值的Go语法表示</td>
<td style="text-align:left"><code>Printf(&quot;%#v&quot;,people)</code></td>
<td style="text-align:left"><code>main.Human{Name:zhangsan}</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%T</code></td>
<td style="text-align:left">相应值<em>类型</em>的Go语法表示</td>
<td style="text-align:left"><code>Printf(&quot;%T&quot;,people)</code></td>
<td style="text-align:left"><code>main.Human</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%%</code></td>
<td style="text-align:left">表示输出一个%(类似转义符)</td>
<td style="text-align:left"><code>Printf(&quot;%%&quot;)</code></td>
<td style="text-align:left"><code>%</code></td>
</tr>
</tbody>
</table>
<p><strong>2. 布尔占位符</strong></p>
<table>
<thead>
<tr>
<th style="text-align:left">占位符</th>
<th style="text-align:left">说明</th>
<th style="text-align:left">举例</th>
<th style="text-align:left">输出</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>%t</code></td>
<td style="text-align:left">输出<code>true</code>或<code>false</code></td>
<td style="text-align:left"><code>Printf(&quot;%t&quot;,true)</code></td>
<td style="text-align:left"><code>true</code></td>
</tr>
</tbody>
</table>
<p><strong>3. 整数占位符</strong></p>
<table>
<thead>
<tr>
<th style="text-align:left">占位符</th>
<th style="text-align:left">说明</th>
<th style="text-align:left">举例</th>
<th style="text-align:left">输出</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>%b</code></td>
<td style="text-align:left">二进制表示</td>
<td style="text-align:left"><code>Printf(&quot;%b&quot;,5)</code></td>
<td style="text-align:left"><code>101</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%d</code></td>
<td style="text-align:left">十进制表示</td>
<td style="text-align:left"><code>Printf(&quot;%d&quot;,0x12)</code></td>
<td style="text-align:left"><code>18</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%o</code></td>
<td style="text-align:left">八进制表示</td>
<td style="text-align:left"><code>Printf(&quot;%o&quot;,10)</code></td>
<td style="text-align:left"><code>12</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%x</code></td>
<td style="text-align:left">小写的十六进制表示</td>
<td style="text-align:left"><code>Printf(&quot;%x&quot;,13)</code></td>
<td style="text-align:left"><code>d</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%X</code></td>
<td style="text-align:left">大写的十六进制表示</td>
<td style="text-align:left"><code>Printf(&quot;%X&quot;,13)</code></td>
<td style="text-align:left"><code>D</code></td>
</tr>
</tbody>
</table>
<p><strong>4. 浮点数和复数的组成部分（实部和虚部）</strong></p>
<table>
<thead>
<tr>
<th style="text-align:left">占位符</th>
<th style="text-align:left">说明</th>
<th style="text-align:left">举例</th>
<th style="text-align:left">输出</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>%b</code></td>
<td style="text-align:left">打印整型的二进制</td>
<td style="text-align:left"><code>Printf(&quot;%b&quot;,10)</code></td>
<td style="text-align:left"><code>1010</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%e</code></td>
<td style="text-align:left">科学计数法，例如：<code>-1234.456e+78</code></td>
<td style="text-align:left"><code>Printf(&quot;%e&quot;,11.909)</code></td>
<td style="text-align:left"><code>1.190900e+01</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%E</code></td>
<td style="text-align:left">科学计数法，例如：<code>-1234.456E+78</code></td>
<td style="text-align:left"><code>Printf(&quot;%e&quot;,11.909)</code></td>
<td style="text-align:left"><code>1.190900E+01</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%f</code></td>
<td style="text-align:left">有小数点后面补足0</td>
<td style="text-align:left"><code>Printf(&quot;%f&quot;,10.2)</code></td>
<td style="text-align:left"><code>10.200000</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%g</code></td>
<td style="text-align:left">还原<code>%e</code>或<code>%E</code>输出的科学计数法类型的内容</td>
<td style="text-align:left"><code>Printf(&quot;%g&quot;,1.190900E+01)</code></td>
<td style="text-align:left"><code>11.909</code></td>
</tr>
</tbody>
</table>
<p><strong>5. 字符串与字节切片</strong></p>
<table>
<thead>
<tr>
<th style="text-align:left">占位符</th>
<th style="text-align:left">说明</th>
<th style="text-align:left">举例</th>
<th style="text-align:left">输出</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>%U</code></td>
<td style="text-align:left">Unicode格式：<code>U+1234</code>，等同于<code>U+%04X</code></td>
<td style="text-align:left"><code>Printf(&quot;%U&quot;,0x4E2D)</code></td>
<td style="text-align:left"><code>U+4E2D</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%c</code></td>
<td style="text-align:left">相应Unicode码所表示的字符</td>
<td style="text-align:left"><code>Printf(&quot;%c&quot;,0x4E2D)</code></td>
<td style="text-align:left"><code>中</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%s</code></td>
<td style="text-align:left">输出字符串表示(接受<code>string</code>、<code>[]byte</code>类型)</td>
<td style="text-align:left"><code>Printf(&quot;%s&quot;,[]byte(&quot;Go语言&quot;))</code></td>
<td style="text-align:left"><code>Go语言</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%q</code></td>
<td style="text-align:left">单引号包括的字符字面值，由Go语法安全转义(参数为Unicode编码时)</td>
<td style="text-align:left"><code>Printf(&quot;%q&quot;,0x4E2D)</code></td>
<td style="text-align:left"><code>&#39;中&#39;</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%q</code></td>
<td style="text-align:left">双引号包括的字符字面值，由Go语法安全转义(参数为string)</td>
<td style="text-align:left"><code>Printf(&quot;%q&quot;,&quot;hehe&quot;)</code></td>
<td style="text-align:left"><code>hehe</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%x</code></td>
<td style="text-align:left">值为字符串时，输出字符的编码(小写)</td>
<td style="text-align:left"><code>Printf(&quot;%x&quot;,&quot;Go语言&quot;)</code></td>
<td style="text-align:left"><code>476fe8afade8a880</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%X</code></td>
<td style="text-align:left">值为字符串时，输出字符的编码(大写)</td>
<td style="text-align:left"><code>Printf(&quot;%X&quot;,&quot;Go语言&quot;)</code></td>
<td style="text-align:left"><code>476FE8AFADE8A880</code></td>
</tr>
</tbody>
</table>
<p><strong>6. 指针</strong>  </p>
<table>
<thead>
<tr>
<th style="text-align:left">占位符</th>
<th style="text-align:left">说明</th>
<th style="text-align:left">举例</th>
<th style="text-align:left">输出</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>%p</code></td>
<td style="text-align:left">输出带0x的指针</td>
<td style="text-align:left"><code>Printf(&quot;%p&quot;,&amp;people)</code></td>
<td style="text-align:left"><code>0xc042040400</code></td>
</tr>
<tr>
<td style="text-align:left"><code>%#p</code></td>
<td style="text-align:left">输出不带0x的指针</td>
<td style="text-align:left"><code>Printf(&quot;%#p&quot;,&amp;people)</code></td>
<td style="text-align:left"><code>c042040400</code></td>
</tr>
</tbody>
</table>
<ul>
<li>转载<a href="http://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter01/01.3.html" target="_blank" rel="noopener">Go语言中文网</a>，重新编辑</li>
</ul>

      
    </div>
    <footer class="entry-meta entry-footer">
      
	<span class="ico-folder"></span>
    <a class="article-category-link" href="/categories/Golang/">Golang</a>

      
  <span class="ico-tags"></span>
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Golang入门/">Golang入门</a></li></ul>

      
        <div id="donation_div"></div>

<script src="/js/vdonate.js"></script>
<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: '/css/images/wechatPay.png',
  alipayImage: '/css/images/alipay.jpg'
});
</script>
      

      
        
	<section id="comments" class="comment">
	  <div id="disqus_thread">
	  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
	  </div>
	</section>

	<script type="text/javascript">
	var disqus_shortname = 'haijdblog';
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	(function(){
	  var dsq = document.createElement('script');
	  dsq.type = 'text/javascript';
	  dsq.async = true;
	  dsq.src = '//' + disqus_shortname + '.disqus.com/count.js';
	  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
	}());
	</script>


      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2017/09/05/Golang文本编码处理/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Golang文本编码处理
        
      </div>
    </a>
  
  
    <a href="/2017/08/11/Go语言基础知识/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Go语言基础知识</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
      <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#fmt包"><span class="nav-number">1.</span> <span class="nav-text">fmt包</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#printf函数"><span class="nav-number">1.1.</span> <span class="nav-text">printf函数</span></a></li></ol></li></ol>
    
    </div>
  </aside>
</section>
        
      </div>
      <footer id="footer" class="site-footer">
  

  <div class="clearfix container">
      <div class="site-info">
	      &copy; 2018 haijd All Rights Reserved.
          
            <span id="busuanzi_container_site_uv">
              本站访客数<span id="busuanzi_value_site_uv"></span>人次  
              本站总访问量<span id="busuanzi_value_site_pv"></span>次
            </span>
          
      </div>
      <div class="site-credit">
        Theme by <a href="https://github.com/iTimeTraveler/hexo-theme-hiero" target="_blank">hiero</a>
      </div>
  </div>
</footer>


<!-- min height -->

<script>
    var contentdiv = document.getElementById("content");

    contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("allheader").offsetHeight - document.getElementById("footer").offsetHeight + "px";
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>
<script src="/js/bootstrap.js"></script>
<script src="/js/main.js"></script>








	<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js" async=""></script>
</body>
</html>
